// pages/admin/dashboard.js
const app = getApp()

Page({
  data: {
    statsData: null,
    chartData: {
      orders: [],
      revenue: [],
      users: []
    },
    timeRange: 'week', // week, month, quarter
    loading: true
  },

  onLoad() {
    this.checkAdminAuth()
    this.loadDashboardData()
  },

  checkAdminAuth() {
    // 简单的管理员权限检查
    const userInfo = app.globalData.userInfo
    if (!userInfo || !userInfo.isAdmin) {
      wx.showModal({
        title: '权限不足',
        content: '需要管理员权限才能访问此页面',
        showCancel: false,
        success: () => {
          wx.navigateBack()
        }
      })
    }
  },

  loadDashboardData() {
    // 模拟统计数据
    const mockStats = {
      totalOrders: 156,
      totalRevenue: 892.5,
      totalUsers: 48,
      activeUsers: 32,
      completionRate: 87.5,
      avgOrderValue: 5.72,
      
      todayOrders: 12,
      todayRevenue: 68.5,
      todayNewUsers: 3,

      orderStatus: {
        pending: 8,
        taken: 15,
        processing: 6,
        completed: 127,
        cancelled: 5
      },

      categoryDistribution: {
        delivery: 45,
        food: 38,
        print: 28,
        rental: 22,
        tutoring: 15,
        other: 8
      }
    }

    // 模拟图表数据
    const mockChartData = {
      orders: this.generateChartData(65, 120),
      revenue: this.generateChartData(320, 850),
      users: this.generateChartData(15, 45)
    }

    this.setData({
      statsData: mockStats,
      chartData: mockChartData,
      loading: false
    })
  },

  generateChartData(min, max) {
    return Array.from({ length: 7 }, () => 
      Math.floor(Math.random() * (max - min + 1)) + min
    )
  },

  changeTimeRange(e) {
    const range = e.currentTarget.dataset.range
    this.setData({
      timeRange: range,
      loading: true
    })

    // 模拟数据重新加载
    setTimeout(() => {
      const newChartData = {
        orders: this.generateChartData(40, 150),
        revenue: this.generateChartData(200, 1000),
        users: this.generateChartData(10, 60)
      }

      this.setData({
        chartData: newChartData,
        loading: false
      })
    }, 500)
  },

  exportData() {
    wx.showModal({
      title: '导出数据',
      content: '确定要导出当前统计数据吗？',
      success: (res) => {
        if (res.confirm) {
          this.handleExport()
        }
      }
    })
  },

  handleExport() {
    wx.showLoading({
      title: '准备导出中...',
    })

    setTimeout(() => {
      wx.hideLoading()
      wx.showToast({
        title: '数据导出成功',
        icon: 'success'
      })
      
      // 实际开发中这里应该调用云函数生成并下载报表
    }, 1000)
  },

  refreshData() {
    this.setData({ loading: true })
    this.loadDashboardData()
  },

  navigateToDetail(e) {
    const type = e.currentTarget.dataset.type
    let url = ''
    
    switch (type) {
      case 'orders':
        url = '/pages/admin/orders/orders'
        break
      case 'users':
        url = '/pages/admin/users/users'
        break
      case 'finance':
        url = '/pages/admin/finance/finance'
        break
      default:
        return
    }

    wx.navigateTo({ url })
  },

  onPullDownRefresh() {
    this.refreshData()
    setTimeout(() => {
      wx.stopPullDownRefresh()
    }, 1000)
  }
})